package xyz.zhzby.cyzx.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import xyz.zhzby.cyzx.cms.convert.TagConvert;
import xyz.zhzby.cyzx.cms.dto.response.Info;
import xyz.zhzby.cyzx.cms.dto.response.TagResponse;
import xyz.zhzby.cyzx.cms.entity.Tag;
import xyz.zhzby.cyzx.cms.mapper.TagMapper;
import xyz.zhzby.cyzx.cms.service.ITagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhzby.cyzx.security.entity.User;
import xyz.zhzby.cyzx.security.util.CyzxSecurityContextHolder;
import xyz.zhzby.cyzx.servicebase.domain.DataLogInfo;
import xyz.zhzby.cyzx.servicebase.domain.RestResponse;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 文章标签（全局设置） 服务实现类
 * </p>
 *
 * @author zby
 * @since 2021-12-10
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements ITagService {

    @Override
    public RestResponse<String> createTag(String tagName) {
        User subject = CyzxSecurityContextHolder.getSubject();
        Tag tag = new Tag();
        tag.setName(tagName);
        subject.fillCreateInfo(tag, DataLogInfo.USER_FIELD_ACTUAL_NAME);
        baseMapper.insert(tag);
        return RestResponse.success("创建成功");
    }

    @Override
    public RestResponse<List<TagResponse>> getAllTags() {
        LambdaQueryWrapper<Tag> qw = new LambdaQueryWrapper<>();
        qw.orderByAsc(Tag::getId);
        List<Tag> tags = baseMapper.selectList(qw);
        return RestResponse.success(TagConvert.INSTANCE.TAG_RESPONSE_LIST(tags));
    }

    @Override
    public List<Info> getLinkTagsByArticleID(Long articleID) {
        return baseMapper.getLinkTagsByArticleID(articleID);
    }
}
